from django.db import connection, transaction


def util_db_select_count(sql):
    """ execute select count(*) sql
        add by sulpha, 2009.11.14
    """
    cursor = connection.cursor()
    cursor.execute(sql)
    rs = cursor.fetchall()
    return rs[0][0]

def util_db_select(sql):
    """ execute select sql
        add by sulpha, 2009.11.22
        TODO: add try throw exception process
    """
    cursor = connection.cursor()
    cursor.execute(sql)
    rs = cursor.fetchall()
    return rs

#def util_db_insert(sql):
    #""" execute insert sql
        #add by sulpha, 2009.11.14
    #"""
    #conn = util_db_connect()
    #cursor = conn.cursor()
    #status = cursor.execute(sql)
    #cursor.close()
    #conn.close()
    #return status
    #cursor = connection.cursor()
    #llll = cursor.execute(sql)
    #print llll
    #transaction.commit_unless_managed()


def util_db_update(sql):
    """ execute update sql
        add by sulpha, 2009.11.14
        TODO: add implement
    """
    conn = util_db_connect()
    cursor = conn.cursor()
    status = cursor.execute(sql)
    cursor.close()
    conn.close()
    print "update status:" + str(status)
    return status


def util_db_connect():
    conn = MySQLdb.connect(host=settings.DATABASE_HOST,
            user=settings.DATABASE_USER, passwd=settings.DATABASE_PASSWORD, 
            db=settings.DATABASE_NAME,
            charset='utf8')
    return conn

